/*递归实现获取无级树数据并生成DOM结构*/

window.tree = function(json) {
	
	if(json == null || json == ""){
		return;
	}
	
	var str = "";
	var forTree = function(o) {
		for (var i = 0; i < o.length; i++) {
			var urlstr = "";
			try {
				if (typeof o[i]["url"] == "undefined") {
					urlstr = "<div><span>";
					if(o[i]["name"].length > 10){
						urlstr += "<font style='font-size:14px;letter-spacing:-1.5px;'>";
						urlstr += o[i]["name"];
						urlstr += "</font>";
					}else{
						urlstr += o[i]["name"];
					}
					
					if (o[i]["list"] != null && o[i]["list"].length > 0) {
						urlstr += "<i class='iconfont icon-right-down'>&#x3125;</i>" ;
					}
					
					urlstr += "</span><ul>";
				} else {
					urlstr += "<div><span>";
					urlstr +=  "<a href=";
					urlstr +=  tree_url+o[i]["url"];
					urlstr +=  ">"
						
					if(o[i]["name"].length > 10){
						urlstr += "<font style='font-size:14px;letter-spacing:-1.5px;'>";
						urlstr += o[i]["name"];
						urlstr += "</font>";
					}else{
						urlstr += o[i]["name"];
					}

					urlstr +=  "</a>" ;
					if (o[i]["list"] != null && o[i]["list"].length > 0) {
						urlstr += "<i class='iconfont icon-right-down'>&#x3125;</i>" ;
					}
					urlstr += "</span><ul>";
				}
				str += urlstr;
				if (o[i]["list"] != null && o[i]["list"].length > 0) {
					forTree(o[i]["list"]);
				}
				str += "</ul></div>";
			} catch (e) {
			}
		}
		return str;
	}
	
	/* 添加无级树 */
	document.getElementById("menuTree").innerHTML = forTree(json);

	/* 树形菜单 */
	var menuTree = function() {
		// 给有子对象的元素加
		$("#menuTree ul").each(function(index, element) {
			var ulContent = $(element).html();
			var spanContent = $(element).siblings("span").html();
			if (ulContent) {
				$(element).siblings("span").html(spanContent)
			}
		});

		$("#menuTree").find("div span").click(function() {
			var ul = $(this).siblings("ul");
			var spanStr = $(this).html();
			var spanContent = spanStr.substr(3, spanStr.length);
			if (ul.find("div").html() != null) {
				if (ul.css("display") == "none") {
					$(this).addClass("icon-close");
					$(this).next().addClass("cf7");
					var pf = $(this).next().css('padding-left');
					$(this).next().css("padding-left", "15px");
					ul.show(300);
				} else {
					ul.hide(300);
					$(this).removeClass("icon-close");
				}
			}
		})
	}()

	/* 树形列表展开 */
	$("#btn_open").click(function() {
		$("#menuTree ul").show(300);
		curzt("-");
	})

	/* 收缩 */
	$("#btn_close").click(function() {
		$("#menuTree ul").hide(300);
		curzt("+");
	})
	function curzt(v) {
		$("#menuTree span").each(function(index, element) {
			var ul = $(this).siblings("ul");
			var spanStr = $(this).html();
			var spanContent = spanStr.substr(3, spanStr.length);
			if (ul.find("div").html() != null) {
				$(this).html("[" + v + "] " + spanContent);
			}
		});
	}
}


window.tree2 = function(json) {
	
	if(json == null || json == ""){
		return;
	}
	
	var str = "";
	var forTree = function(data) {
		$.each(data,function(n,o) {
			for (var i = 0; i < o.length; i++) {
				var urlstr = "";
				try {
					if (typeof o[i]["url"] == "undefined") {
						urlstr = "<div><span>"
								+ o[i]["name"]
								+ "<i class='iconfont icon-right-down'>&#x3125;</i></span><ul>";
					} else {
						urlstr = "<div><span>"
								+ "<a href="
								+ tree_url+o[i]["url"]
								+ ">"
								+ o[i]["name"]
								+ "</a><i class='iconfont icon-right-down'>&#x3125;</i></span><ul>";
					}
					str += urlstr;
					if (o[i]["list"] != null && o[i]["list"].length > 0) {
						forTree(o[i]["list"]);
					}
					str += "</ul></div>";
				} catch (e) {
				}
			}
		});
		return str;
	}
	
	/* 添加无级树 */
	document.getElementById("menuTree").innerHTML = forTree(json);

	/* 树形菜单 */
	var menuTree = function() {
		// 给有子对象的元素加
		$("#menuTree ul").each(function(index, element) {
			var ulContent = $(element).html();
			var spanContent = $(element).siblings("span").html();
			if (ulContent) {
				$(element).siblings("span").html(spanContent)
			}
		});

		$("#menuTree").find("div span").click(function() {
			var ul = $(this).siblings("ul");
			var spanStr = $(this).html();
			var spanContent = spanStr.substr(3, spanStr.length);
			if (ul.find("div").html() != null) {
				if (ul.css("display") == "none") {
					$(this).addClass("icon-close");
					$(this).next().addClass("cf7");
					var pf = $(this).next().css('padding-left');
					$(this).next().css("padding-left", "15px");
					ul.show(300);
				} else {
					ul.hide(300);
					$(this).removeClass("icon-close");
				}
			}
		})
	}()

	/* 树形列表展开 */
	$("#btn_open").click(function() {
		$("#menuTree ul").show(300);
		curzt("-");
	})

	/* 收缩 */
	$("#btn_close").click(function() {
		$("#menuTree ul").hide(300);
		curzt("+");
	})
	function curzt(v) {
		$("#menuTree span").each(function(index, element) {
			var ul = $(this).siblings("ul");
			var spanStr = $(this).html();
			var spanContent = spanStr.substr(3, spanStr.length);
			if (ul.find("div").html() != null) {
				$(this).html("[" + v + "] " + spanContent);
			}
		});
	}
}
